import Cookies from 'js-cookie'

const PAGE_SIZE_KEY = 'paginationSize'
const SIDE_BAR_OPEN_KEY = 'sidebarStatus'
const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get(SIDE_BAR_OPEN_KEY)
    },
    pagination: {
      size: parseInt(Cookies.get(PAGE_SIZE_KEY)) || 10,
      options: [10, 20, 50, 100]
    }
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set(SIDE_BAR_OPEN_KEY, 1)
      } else {
        Cookies.set(SIDE_BAR_OPEN_KEY, 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    CHANGE_PAGE_SIZE: (state, size) => {
      state.pagination.size = size
      Cookies.set(PAGE_SIZE_KEY, size)
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    ChangePageSize: ({ commit }, size) => {
      commit('CHANGE_PAGE_SIZE', size)
    }
  }
}

export default app
